﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using QuanLyHocPhi.BLL;
using QuanLyHocPhi.Model;

namespace QuanLyHocPhi.GUI.DuLieu
{
    public partial class FormSelectLopGoc : Form
    {
        [System.ComponentModel.DataAnnotations.Schema.NotMapped]
        private class LopGocSelection : LopGoc
        {
            public bool Checked { get; set; }
            public string TenLoaiHinh
            {
                get
                {
                    try
                    {
                        return this.LoaiHinhDaoTao.TenLoaiHinh;
                    }
                    catch
                    {
                        return "?";
                    }
                }
            }
        }

        private List<string> m_MaLopGocExcludedList = null;

        public List<LopGoc> SelectedLopGocList
        {
            get
            {
                var lstLopGoc = new List<LopGoc>();
                if (bsLopGoc.DataSource != null)
                {
                    var lstLopGocSelection = bsLopGoc.DataSource as List<LopGocSelection>;
                    lstLopGoc = lstLopGocSelection.Where(e => e.Checked).
                        Select(e => new LopGoc()
                        {
                            MaLopGoc = e.MaLopGoc,
                            TenLopGoc = e.TenLopGoc,
                            MaLoaiHinhDaoTao = e.MaLoaiHinhDaoTao,
                            LoaiHinhDaoTao = e.LoaiHinhDaoTao,
                            NamTuyenSinh = e.NamTuyenSinh
                        }).ToList();
                }
                return lstLopGoc;
            }
        }

        #region SelectedLoaiHinhDaoTao
        private LoaiHinhDaoTao SelectedLoaiHinhDaoTao
        {
            get
            {
                if (bsLoaiHinhDaoTao.Current == null)
                    return null;
                else
                    return bsLoaiHinhDaoTao.Current as LoaiHinhDaoTao;
            }
        }

        private string SelectedMaLoaiHinhDaoTao
        {
            get
            {
                var loaiHinh = this.SelectedLoaiHinhDaoTao;
                if (loaiHinh == null)
                    return null;
                else
                    return loaiHinh.MaLoaiHinh;
            }
        }
        #endregion

        #region SelectedNamTuyenSinh
        private string SelectedNamTuyenSinh
        {
            get
            {
                if (bsNamTuyenSinh.Current == null)
                    return null;
                else
                    return bsNamTuyenSinh.Current.ToString();
            }
        }
        #endregion

        public FormSelectLopGoc(List<string> lstExcludedMaLopGocList = null)
        {
            InitializeComponent();
            if (lstExcludedMaLopGocList == null)
                lstExcludedMaLopGocList = new List<string>();
            m_MaLopGocExcludedList = lstExcludedMaLopGocList;
        }

        private void bsLoaiHinhDaoTao_CurrentChanged(object sender, EventArgs e)
        {
            if (m_NeedLoadLopGocList)
                NapDanhSachLopGoc();
        }

        private void bsNamTuyenSinh_CurrentChanged(object sender, EventArgs e)
        {
            if (m_NeedLoadLopGocList)
                NapDanhSachLopGoc();
        }

        private void FormSelectLopGoc_Load(object sender, EventArgs e)
        {
            m_NeedLoadLopGocList = false;
            NapDanhSachLoaiHinhDaoTao();
            NapDanhSachNamTuyenSinh();
            chkLoaiHinhDaoTao.Checked = chkNamTuyenSinh.Checked = true;
            m_NeedLoadLopGocList = true;
            NapDanhSachLopGoc();
        }

        private bool m_NeedLoadLopGocList = true;
        private void NapDanhSachLoaiHinhDaoTao(string maLoaiHinhToFocus = null)
        {
            var lstLoaiHinh = BizLoaiHinhDaoTao.GetListLoaiHinhDaoTao();
            bsLoaiHinhDaoTao.DataSource = lstLoaiHinh;
            for (int i = 0; i < lstLoaiHinh.Count; i++)
                if (lstLoaiHinh[i].MaLoaiHinh == maLoaiHinhToFocus)
                {
                    bsLoaiHinhDaoTao.Position = i;
                    break;
                }
            cbxLoaiHinhDaoTao.DisplayMember = "TenLoaiHinh";
            cbxLoaiHinhDaoTao.DataSource = bsLoaiHinhDaoTao;
        }

        private void NapDanhSachNamTuyenSinh(string namTuyenSinhToFocus = null)
        {
            var lst = BizLopGoc.GetListNamTuyenSinh();
            bsNamTuyenSinh.DataSource = lst;
            for (int i = 0; i < lst.Count; i++)
                if (lst[i] == namTuyenSinhToFocus)
                {
                    bsNamTuyenSinh.Position = i;
                }
            cbxNamTuyenSinh.DataSource = bsNamTuyenSinh;
        }

        private void NapDanhSachLopGoc()
        {
            // Nạp danh sách lớp gốc theo điều kiện đang chọn
            gridLopGoc.DataSource = null;

            LoaiHinhDaoTao loaiHinh = chkLoaiHinhDaoTao.Checked ? this.SelectedLoaiHinhDaoTao : null;
            string namTuyenSinh = chkNamTuyenSinh.Checked ? this.SelectedNamTuyenSinh : null;
            var lstLopGoc = BizLopGoc.GetListLopGoc(loaiHinh, namTuyenSinh);
            List<LopGocSelection> lstLopGoc2 = null;
            lstLopGoc2 = lstLopGoc.Where(e => m_MaLopGocExcludedList.Contains(e.MaLopGoc) == false).Select(e =>
                new LopGocSelection()
                {
                    Checked = false,
                    MaLopGoc = e.MaLopGoc,
                    TenLopGoc = e.TenLopGoc,
                    LoaiHinhDaoTao = e.LoaiHinhDaoTao,
                    MaLoaiHinhDaoTao = e.MaLoaiHinhDaoTao,
                    NamTuyenSinh = e.NamTuyenSinh
                }).ToList();

            bsLopGoc.DataSource = lstLopGoc2;
            gridLopGoc.AutoGenerateColumns = false;
            gridLopGoc.DataSource = bsLopGoc;
        }

        private void chkLoaiHinhDaoTao_CheckedChanged(object sender, EventArgs e)
        {
            cbxLoaiHinhDaoTao.Enabled = chkLoaiHinhDaoTao.Checked;
            if (m_NeedLoadLopGocList)
                NapDanhSachLopGoc();
        }

        private void chkNamTuyenSinh_CheckedChanged(object sender, EventArgs e)
        {
            cbxNamTuyenSinh.Enabled = chkNamTuyenSinh.Checked;
            if (m_NeedLoadLopGocList)
                NapDanhSachLopGoc();
        }

        private void gridLopGoc_ColumnHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            if (e.ColumnIndex == colLopGoc_Selected.Index)
            {
                bsLopGoc.EndEdit();

                // Đảo ngược chọn lựa
                for (int i = 0; i < gridLopGoc.Rows.Count; i++)
                {
                    var row = gridLopGoc.Rows[i];
                    var lop = row.DataBoundItem as LopGocSelection;
                    lop.Checked = !lop.Checked;
                }

                bsLopGoc.ResetBindings(false);
            }
        }

        private void lnkCheckAll_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            bsLopGoc.EndEdit();
            for (int i = 0; i < gridLopGoc.Rows.Count; i++)
            {
                var row = gridLopGoc.Rows[i];
                var lop = row.DataBoundItem as LopGocSelection;
                lop.Checked = true;
            }
            bsLopGoc.ResetBindings(false);
        }
    }
}
